package com.yunxun.coinmessage.service;

import com.yunxun.coinmessage.dao.ClassifiedPacketDao;
import com.yunxun.coinmessage.dao.MesSourceDao;
import com.yunxun.coinmessage.entity.ClassifiedPacket;
import com.yunxun.coinmessage.entity.MesSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ClassifiedPacketService {
    @Autowired
    ClassifiedPacketDao classifiedPacketDao;
    @Autowired
    MesSourceDao mesSourceDao;
    public  List<ClassifiedPacket> findAll(){
        List<ClassifiedPacket> list=classifiedPacketDao.findAll();
        return list;
    }
    public  Integer getTotal(){
        return classifiedPacketDao.getTotal();
    }
    //增加分类
    public String add(ClassifiedPacket classifiedPacket){

        if (classifiedPacketDao.findByName(classifiedPacket.getPname()) != 0) {
            return "分类已存在";
        }
        classifiedPacketDao.add(classifiedPacket);
          return "添加成功";
    }

    /**
     * 如果该分类下还有渠道，则不让删除
     * @param pid
     */
    public String deleteById(Integer pid){
        List<MesSource> mesSourceList=mesSourceDao.queryByClassifiedPacket(pid);
        if(mesSourceList.size()!=0){
            return "该分类下还有渠道，请先删除渠道";
        }
        classifiedPacketDao.deleteById(pid);
        return "删除成功";
    }
    public  ClassifiedPacket findById(Integer pid){
        return  classifiedPacketDao.findById(pid);
    }

    public String updateById(ClassifiedPacket classifiedPacket){
        Integer a=null;
         a=classifiedPacketDao.updateById(classifiedPacket);
         if(a!=null){
             return "更新分类成功";
         }
         return "更新分类失败，请重新尝试";
    }
}
